﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using Data;
using DevExpress.XtraEditors;
using Event;

namespace HTTPRequest2
{
    public partial class Address : UserControl
    {
        public Address()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 保存事件
        /// </summary>
        public event Event.EventGlobal.DeSave EvSave;


        /// <summary>
        /// 请求事件
        /// </summary>
        public event Event.EventGlobal.DeSend EvSend;

        private RequestParameteData rpd = new RequestParameteData();

        public void BuildAddress(RequestParameteData _rpd)
        {
            this.rpd = _rpd;
            this.lookUpEdit_地址.EditValue = _rpd.requestUrl;
            this.comboBoxEdit_请求类型.EditValue = _rpd.requestType;
            this.barEditItem_读取超时.EditValue = _rpd.requestReadOutTime;
            this.barEditItem_写入超时.EditValue = _rpd.requestWriteOutTime;
            RefreshDataSource();
        }

        private void simpleButton_保存_Click(object sender, EventArgs e)
        {
            if (EvSave != null)
            {
                EvSave();
            }
        }
        private void simpleButton_地址管理_Click(object sender, EventArgs e)
        {
            AddressEditorFrom fa = new AddressEditorFrom(rpd);
            fa.FormClosing += new FormClosingEventHandler(AddressEditor_FormClosing);
            fa.ShowDialog();
        }

        private void dropDownButton_请求_Click(object sender, EventArgs e)
        {
            if (EvSend != null)
            {
                EvSend();
            }
        }

        /// <summary>
        /// 地址管理窗口-关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddressEditor_FormClosing(object sender, FormClosingEventArgs e)
        {
            RefreshDataSource();
        }

        private void RefreshDataSource()
        {
            this.lookUpEdit_地址.Properties.DisplayMember = "Value";
            this.lookUpEdit_地址.Properties.ValueMember = "Value";
            this.lookUpEdit_地址.Properties.DataSource = rpd.urls;
        }

        private void comboBoxEdit_请求类型_EditValueChanged(object sender, EventArgs e)
        {
            string f = (string)this.comboBoxEdit_请求类型.EditValue;
            rpd.requestType = f;
            ParameterItemNodeData pind = rpd.requestHeads.Find(x => x.Key == "Method");
            if(pind != null){
                pind.Value = f;
                rpd.requestType = f;
            }            
            Console.WriteLine(rpd.requestType);
        }

        private void lookUpEdit_地址_EditValueChanged(object sender, EventArgs e)
        {
            string f = (string)this.lookUpEdit_地址.EditValue;
            rpd.requestUrl = f;
            Console.WriteLine(rpd.requestUrl);
        }

        private void repositoryItemTextEdit_读取超时_EditValueChanged(object sender, EventArgs e)
        {
            TextEdit te = (TextEdit)sender;
            int number;
            bool result = Int32.TryParse(te.Text, out number); // return bool value hint y/n
            if (result)
            {
                rpd.requestReadOutTime = number;
                Console.WriteLine(rpd.requestReadOutTime.ToString());
            }
            else
            {
                te.Text = rpd.requestReadOutTime.ToString();
            }
        }

        private void repositoryItemTextEdit_写入超时_EditValueChanged(object sender, EventArgs e)
        {
            TextEdit te = (TextEdit)sender;
            int number;
            bool result = Int32.TryParse(te.Text, out number); // return bool value hint y/n
            if (result)
            {
                rpd.requestWriteOutTime = number;
                Console.WriteLine(rpd.requestWriteOutTime.ToString());
            }
            else
            {
                te.Text = rpd.requestWriteOutTime.ToString();
            }
        }

        


    }
}
